package com.ruoyi.web.controller.transaction;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.transaction.service.IContactService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.transaction.domain.Contact;
import java.util.List;

/**
 * Created by gzdmt01 on 2019/3/28.
 */
@Api(tags="地址管理")
@Controller
@RequestMapping("/transaction/contact")
public class contactController extends BaseController {
    private String prefix = "/transaction/contact";

    @Autowired
    private IContactService contactService;

    @Log(title = "新增地址",businessType = BusinessType.INSERT)
    @ApiOperation("新增地址")
    @ApiImplicitParam(name = "contact", value = "地址信息", dataType = "Contact")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody Contact contact){
        System.out.println(contact);
        Long userId = ShiroUtils.getUserId();
        contact.setUserId(Integer.parseInt(userId+""));
        return contactService.add(contact);
    }

    @GetMapping("list")
    public String listByUserId(ModelMap map){
        Long userId = ShiroUtils.getUserId();
        List<Contact> list = contactService.listByUserId(Integer.parseInt(userId+""));
        map.put("list",list);
        return prefix + "/contact";
    }

    @PostMapping("list/{contactId}")
    @ResponseBody
    public Contact ListByContactId(@PathVariable("contactId") Integer contactId){
        Long userId = ShiroUtils.getUserId();
        Contact contact = contactService.listByContactId(contactId,Integer.parseInt(userId+""));
        return contact;
    }

    @PostMapping("changeDefaultContact/{contactId}")
    @ResponseBody
    public AjaxResult changeDefaultContact(@PathVariable("contactId") Integer contactId){
        Long userId = ShiroUtils.getUserId();
        return contactService.changeDefaultContact(contactId,Integer.parseInt(userId+""));
    }

}
